<?php

namespace App\Model;


class AuditModel extends BaseModel
{
    protected $table = 'audit';
    public $timestamps = false;
    protected $guarded = [];

    //关联card表
    public function card(){
        return $this ->hasMany(CardModel::class,'id','cid');
    }
    //关联图片表
    public function image(){
        return $this ->hasMany(ImageModel::class,'card_id','cid');
    }

    public function getAuditCard($type)
    {

      $res =   AuditModel::
      with([
          'card' =>function($query){
            $query  -> select(['u_nickname','head_img','school_name','id','sid','content']) -> where(['is_delete' => 0]);
          },
          'image' =>function($query){
              $query ->select(['id','img','card_id']);
          }
      ]) ->
      where(['type' =>$type,'is_dispose' => 0]) ->paginate(10);

        if($res){
            return ['code'=>true,'data' => $res];
        }else{
            return ['code'=>false,'data' => $res];
        }

    }

    public function DeletedAuditCard($aid, $cid)
    {

       $audit =  AuditModel::find($aid);
       if(is_null($audit)){
           return ['code'=>false,'data'=>'没有找到相关数据!'];
       }
       $audit ->is_dispose = 1;
       $audit ->save();

      $card =  CardModel::find($cid);
      if (is_null($card)){
          return ['code'=>false,'data'=>'没有找到相关数据！'];
      }
      $card ->is_delete = 1;
      $card -> save();

      if($audit && $card){
          return ['code'=>true,'data'=>1];
      }else{
          return ['code'=>false,'data'=>0];

      }

    }

    public function AuditCard($aid)
    {
        $audit =  AuditModel::find($aid);
        if(is_null($audit)){
            return ['code'=>false,'data'=>'没有找到相关数据!'];
        }
        $audit ->is_dispose = 1;
        $audit ->save();


        if($audit ){
            return ['code'=>true,'data'=>1];
        }else{
            return ['code'=>false,'data'=>0];

        }
    }

    public function reportCard($type, $cid, $uid)
    {
        $audit =  AuditModel::create([
            'type' =>$type,
            'cid' =>$cid,
            'uid' =>$uid,
            'created_at' =>date('Y-h-d H:i:s'),
        ]);
        if($audit){
            return ['code'=>true,'data'=>1];
        }else{
            return ['code'=>false,'data'=>0];

        }

    }


}
